In my earlier analysis, I delved into the Sales_Export_2019_2020 dataset, conducting data exploration and cleaning. In this phase, I will generate visualizations aimed at gaining insights into various aspects:
I intend to identify the countries with the highest cost and order values, shedding light on their respective standings.
Additionally, I plan to investigate the discrepancies in cost and order values across different categories within each country, providing a comprehensive view of category-specific performance.
Furthermore, I aim to examine the cumulative profit earned within each country and category, providing a holistic perspective on profitability.
By creating these visualizations, I hope to derive meaningful patterns and trends that will contribute to a more profound understanding of the dataset and its underlying dynamics.
#Smoothed scatter plot of order values in Euros against countries using the #Sales_Export_2019_2020 dataset.
#
orderval <- ggplot(data = Sales_Export_2019_2020, aes(x = country, y = order_value_EUR, color = category, fill = category)) + geom_smooth(method = 'loess', formula = 'y ~ x', aes(group = category)) + scale_y_continuous(labels = scales::comma) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Order Values in Each Country (2019-2020))", x = "Country", y = "Order Value (EURO)")
#converting ggplot graph into a interactive Plotly graph, with a customized tooltip.
p <- ggplotly(orderval, tooltip = c("x", "y", "color"))
#Displaying Plotly graph.
p
I utilize the ggplot function with our Sales_Export_2019_2020 data to create a visualization. To establish visual characteristics, I make use of the aes function, with the x-axis displaying the country variable and the y-axis representing “Order_value_EUR.” Furthermore, I associate the “category” variable with color and fill aesthetics to differentiate between visual categories.
I create a smoothed line on the plot using the geom_smooth function with the “loess” method. The group aesthetic is set to “category” to ensure each category has its own smoothed line, and the smoothing equation is outlined in the formula parameter.
To enhance the legibility of x-axis text labels, I use the theme function to adjust the angle to 45 degrees and align the labels to the right using the hjust = 1 setting. I also format the y-axis labels using the scale_y_continuous function with the scales::comma function to add commas for better readability.
To add a title and axis labels, I use the labs function. Lastly, I convert the ggplot visualization (orderval) into an interactive Plotly graph with the ggplotly function, and customize the tooltip parameter to display the relevant information when hovering over data points.
The resulting interactive Plotly graph (p) provides an insightful visualization of order values across various countries, enriched with interactivity and tailor-made tooltips.
#Grouped column chart to visualize the total sales of each country's exports by category
#using the CountryTotalSaleExport dataset.
#Each country is represented on the x-axis, and the total sale values are shown as grouped #columns with different colors representing categories.
TCCSales <- ggplot(data = CountryTotalSaleExport, aes(x = country, y = saleCategoryTotal, fill = category)) + geom_col() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Total Sales of Each Countries Exports by Category (2019-2020))", x = "Country", y = "Cost Total") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into an interactive plotly graph.
tcc <- ggplotly(TCCSales)
#Displaying Plotly graph.
tcc
#Scatter plot with connected lines to visualize the total cost of each country's exports by #category using the CountryTotalcostExport dataset.
#Each country is represented on the x-axis, and the total cost values are shown as scatter #points with lines connecting points of the same category.
TCCCost <- ggplot(data = CountryTotalcostExport, aes(x = country, y = costCategoryTotal, colour = category)) + geom_point() + geom_line(aes(group = category)) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Total Cost of Each Countries Exports by Category (2019-2020))", x = "Country", y = "Cost Total (Euros)") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into interactive Plotly graph.
TCCC <- ggplotly(TCCCost)
#Displaying Plotly Graph.
TCCC
#Scatter plot to visualize the cross profit per country using the CountryProfit dataset.
#Each country is represented on the x-axis, and the total gross profit values are shown as scatter points.
#The x-axis labels are reordered based on the total gross profit values, and the plot #includes axis labels, title, and formatted y-axis labels.
CGProfit <- ggplot(data = CountryProfit, aes(x = reorder(country, GrossProfit), y = GrossProfit)) + geom_point() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Gross Profit Per Country (2019-2020))", x = "Country", y = "Total Gross Profit (Euros)") + scale_y_continuous(labels = scales::comma)
#Converting ggplot graph into interactive Plotly graph, with customized tooltip.
CG <- ggplotly(CGProfit, tooltip = c("x", "y"))
#Displaying Plotly Graph
CG
#Heatmap to visualize gross profit by category using the CategoryProfit dataset.
#Each category is represented on the x-axis, and the gross profit values are shown as #colored tiles.
CProfit <- ggplot(data = CategoryProfit, aes(x = category, y = GrossProfit, fill = category)) + geom_tile(aes()) + scale_y_continuous(labels = scales::comma) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = "Gross Profit by Category (2019 - 2020", x = "Category", y = "Gross Profit (Euros)")
#Convert ggplot graph into interactive Plotly graph, with customized tooltip.
CP <- ggplotly(CProfit, tooltip = c("x", "y"))
#Display Plotly graph.
CP
library(shiny)
## Warning: package 'shiny' was built under R version 4.2.3
##
## Attaching package: 'shiny'
## The following objects are masked from 'package:DT':
##
## dataTableOutput, renderDataTable
# Define UI
ui <- fluidPage(
titlePanel("Interactive Pie Chart"),
sidebarLayout(
sidebarPanel(
selectInput("country", "Select Country:", choices = unique(CoProfit$country))
),
mainPanel(
plotlyOutput("pieChart")
)
)
)
# Define server
server <- function(input, output) {
output$pieChart <- renderPlotly({
country_data <- CoProfit %>%
filter(country == input$country)
labels <- country_data$category
values <- country_data$GProfit
pie_chart <- plot_ly(type = 'pie', labels = labels, values = values,
textinfo = 'label+percent',
insidetextorientation = 'radial')
pie_chart <- pie_chart %>%
layout(title = paste("Total Gross Profit based on Category Contribution -", input$country),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
pie_chart
})
}
# Run the Shiny app
shinyApp(ui = ui, server = server)
##
## Listening on http://127.0.0.1:8760